from 数据库操作.CreateMysqlCon import CreateMysqlCon

class FollowOperation:
    def __init__(self):
        self.con = CreateMysqlCon().get_con()
        if not self.con:
            raise ConnectionError("数据库连接失败，请检查配置或网络")
        self.cursor = self.con.cursor()

    def follow_user(self, follower_id, followed_id):
        """关注用户"""
        try:
            # 不能关注自己
            if follower_id == followed_id:
                return False
            
            # 检查是否已经关注
            self.cursor.execute(
                "SELECT ID FROM user_follows WHERE follower_id = %s AND followed_id = %s", 
                (follower_id, followed_id)
            )
            if self.cursor.fetchone():
                return True  # 已经关注过了
            
            sql = "INSERT INTO user_follows (follower_id, followed_id) VALUES (%s, %s)"
            self.cursor.execute(sql, (follower_id, followed_id))
            self.con.commit()
            return True
        except Exception as e:
            print(f"关注用户失败: {e}")
            return False

    def unfollow_user(self, follower_id, followed_id):
        """取消关注"""
        try:
            sql = "DELETE FROM user_follows WHERE follower_id = %s AND followed_id = %s"
            self.cursor.execute(sql, (follower_id, followed_id))
            self.con.commit()
            return True
        except Exception as e:
            print(f"取消关注失败: {e}")
            return False

    def get_followers(self, user_id):
        """获取用户的粉丝列表"""
        try:
            sql = """SELECT u.ID, u.username, f.follow_time 
                   FROM user_follows f 
                   JOIN user_data u ON f.follower_id = u.ID 
                   WHERE f.followed_id = %s"""
            self.cursor.execute(sql, (user_id,))
            return self.cursor.fetchall()
        except Exception as e:
            print(f"获取粉丝列表失败: {e}")
            return []

    def get_following(self, user_id):
        """获取用户关注的人列表"""
        try:
            sql = """SELECT u.ID, u.username, f.follow_time 
                   FROM user_follows f 
                   JOIN user_data u ON f.followed_id = u.ID 
                   WHERE f.follower_id = %s"""
            self.cursor.execute(sql, (user_id,))
            return self.cursor.fetchall()
        except Exception as e:
            print(f"获取关注列表失败: {e}")
            return []

    def is_following(self, follower_id, followed_id):
        """检查是否已关注"""
        try:
            sql = "SELECT ID FROM user_follows WHERE follower_id = %s AND followed_id = %s"
            self.cursor.execute(sql, (follower_id, followed_id))
            return bool(self.cursor.fetchone())
        except Exception as e:
            print(f"检查关注状态失败: {e}")
            return False

    def get_followed_blogs(self, user_id):
        """获取关注用户的博客列表"""
        try:
            sql = """SELECT b.ID, b.title, u.username, b.details, b.publish_date 
                   FROM blog_list b 
                   JOIN user_data u ON b.user_id = u.ID 
                   JOIN user_follows f ON u.ID = f.followed_id 
                   WHERE f.follower_id = %s 
                   ORDER BY b.publish_date DESC"""
            self.cursor.execute(sql, (user_id,))
            return self.cursor.fetchall()
        except Exception as e:
            print(f"获取关注用户博客失败: {e}")
            return []